package com.treasure.mall.biz.entity.auction;

import com.treasure.mall.biz.entity.BasePO;
import com.treasure.mall.biz.entity.product.ProductPO;
import com.treasure.mall.zbq.common.constant.Constant;
import com.treasure.mall.zbq.common.ienum.auction.AuctionEnums;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 拍卖场次
 *
 * @author allan
 * @version 1.0.0
 * @date 2020/7/16
 */
@Entity
@Table(name = "zb_auction_session")
@Setter
@Getter
@Cacheable(false)
public class AuctionSessionPO extends BasePO {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;
    @ManyToOne
    @JoinColumn(name = "product_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private ProductPO productPO;

    /**
     * 房间ID
     */
    @Column(name = "room_id")
    private Long roomId;

    /**
     * 拍卖开始时间
     */
    @Column(name = "start_time", columnDefinition = Constant.DATE_COLUMN_DEFINITION)
    private LocalDateTime startTime;
    @Column(name = "auction_status")
    private AuctionEnums.AuctionStatus auctionStatus;
    /**
     * 结束时间
     */
    @Column(name = "end_time", columnDefinition = Constant.DATE_COLUMN_DEFINITION)
    private LocalDateTime endTime;
    /**
     * 起拍价
     */
    @Column(name = "starting_price", scale = 2, precision = 10)
    private BigDecimal startingPrice;
    /**
     * 单位加价幅度
     */
    @Column(name = "unit_increase", scale = 2, precision = 9)
    private BigDecimal unitIncrease;
    /**
     * 保证金
     */
    @Column(name = "security_deposit", scale = 2, precision = 9)
    private BigDecimal securityDeposit;
    /**
     * 拍卖成功者
     */
    @ManyToOne
    @JoinColumn(name = "winner_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private AuctionBidPO winner;
    /**
     * 是否延时
     * 是：倒计时结束10秒内如果有新的出价会延长截拍时间，每次延长10秒，系统最多只延长3次
     */
    @Column(name = "is_delay")
    private boolean delay;

    public BigDecimal currentPrice() {
        if (winner == null) {
            return startingPrice;
        }
        return winner.getBidAmount();
    }
}
